package com.skytech.component.monitor.util;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ElementAbstractUrl {
    public static void transform(Elements elements){
        for (Element element : elements) {
            // 将页面的相对路径转化为绝对路径
            String src = element.absUrl("src");
            String href = element.absUrl("href");
            if(!StringUtils.isEmpty(src)){
                element.attr("src",src);
            }
            if(!StringUtils.isEmpty(href)){
                element.attr("href",href);
            }
        }
    }

    public static List<String> links(Elements elements){
        Set<String> result = new HashSet<>();
        /*List<String> result = new ArrayList<>();*/
        for (Element element : elements) {
            if(element.tag().getName().equals("a")&&element.hasAttr("href")){
                result.add(element.absUrl("href"));
            }

        }
        return result.stream().collect(Collectors.toList());
    }
}
